package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"user-mgr/cache"
	"user-mgr/models"
	"user-mgr/pkg"
)

type UserController struct {
	beego.Controller
}

type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (u *UserController) Login() {

	username := u.GetString("username")
	password := u.GetString("password")
	if username == "" || password == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名或密码为空",
		}
		u.ServeJSON()
		return
	}
	userLogin := "user_login"
	errCount, _ := cache.Rdb.Get(cache.Ctx, userLogin).Int()
	if errCount > 3 {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误3次",
		}
		u.ServeJSON()
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户不存在",
		}
		u.ServeJSON()
		return
	}
	if user.Password != pkg.Md5Str(password) {
		cache.Rdb.Set(cache.Ctx, userLogin, 3, time.Hour*2)
		if errCount > 2 {
			u.Data["json"] = Res{
				Code: 0,
				Msg:  "密码错误3次，封禁2小时",
			}
			u.ServeJSON()
			return

		}
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误",
		}
		u.ServeJSON()
		return
	}
	u.Data["json"] = Res{
		Code: 200,
		Msg:  "登录成功",
		Data: user,
	}
	u.ServeJSON()
	return

}
func (u *UserController) DoneAdd() {

	name := u.GetString("name")
	if name == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "角色名称为空",
		}
		u.ServeJSON()
		return
	}
	names := u.GetString("names")
	done := models.Done{
		Names: names,
	}
	err := models.DB.Create(&done).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "权限添加失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "权限添加成功",
		}
		u.ServeJSON()
	}
}
func (u *UserController) RoleList() {

	username := u.GetString("username")
	if username == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名为空",
		}
		u.ServeJSON()
		return
	}
	var role models.Role
	err := models.DB.First(&role).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: role,
		}
		u.ServeJSON()
	}
}
func (u *UserController) DoneList() {

	name := u.GetString("name")
	if name == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "角色名为空",
		}
		u.ServeJSON()
		return
	}
	var done []models.Done
	err := models.DB.First(&done).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: done,
		}
		u.ServeJSON()
	}

}
func (u *UserController) RoleAdd() {

	name := u.GetString("name")
	role := models.Role{
		Name: name,
	}
	err := models.DB.Create(&role).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "角色创建失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "角色创建成功",
		}
		u.ServeJSON()
	}
}
func (u *UserController) UserAdd() {

	username := u.GetString("username")
	password := u.GetString("password")
	name := u.GetString("name")
	if username == "" || password == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名或密码为空",
		}
		u.ServeJSON()
		return
	}
	if name == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "角色为空",
		}
		u.ServeJSON()
		return
	}
	user := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户注册失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "用户注册成功",
		}
		u.ServeJSON()
	}
}
func (u *UserController) UserList() {

	page, _ := u.GetInt("page", 1)
	size, _ := u.GetInt("size", 3)
	offset := (page - 1) * size
	query := models.DB.Model(&models.User{})
	var count int64
	query.Count(&count)
	var user []models.User
	userList := "user_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, userList).Result()
	if cacheData == "" {
		err := query.Offset(offset).Limit(size).Find(&user).Error
		if err != nil {
			u.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			u.ServeJSON()
			return
		}
		cacheList, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, userList, cacheList, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	PageData := make(map[string]interface{})
	PageData["count"] = count
	PageData["user"] = user
	u.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: PageData,
	}
	u.ServeJSON()

}
func (u *UserController) UserDel() {

	id, _ := u.GetInt("id")
	err := models.DB.Delete(&models.User{}, id).Error
	if err != nil {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
		}
		u.ServeJSON()
	}
}
func (u *UserController) UserShow() {

	id, _ := u.GetInt("id")
	var user models.User
	userShow := "user_show"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, userShow).Result()
	if cacheData == "" {
		err := models.DB.Where("id=?", id).First(&user).Error
		if err != nil {
			u.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			u.ServeJSON()
			return
		}
		cacheShow, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, userShow, cacheShow, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	u.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: user,
	}
	u.ServeJSON()
}
